export function getAssetsImages(url) {
  return new URL(`/src/assets/image/${url}`, import.meta.url).href
}

export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  if (!time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (typeof time === 'string') {
      if (/^[0-9]+$/.test(time)) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

export function useDownload(data, name, type) {
  // const url = data + '?response-content-type=application/octet-stream'
  // const url = data + '?response-content-type=application/pdf&报告.pdf'
  const url = type == 'url' ? data : window.URL.createObjectURL(new Blob([data]))
  const link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', `${name}`)
  document.body.appendChild(link)
  link.click()
}
// 日期格式化
export function formatDate(formatStr, fdate) {
  if (fdate) {
    if (~fdate.indexOf('.')) {
      fdate = fdate.substring(0, fdate.indexOf('.'))
    }
    fdate = fdate.toString().replace('T', ' ').replace(/\-/g, '/')
    var fTime,
      fStr = 'ymdhis'
    if (!formatStr) formatStr = 'y-m-d h:i:s'
    if (fdate) fTime = new Date(fdate)
    else fTime = new Date()
    var month = fTime.getMonth() + 1
    var day = fTime.getDate()
    var hours = fTime.getHours()
    var minu = fTime.getMinutes()
    var second = fTime.getSeconds()
    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day
    hours = hours < 10 ? '0' + hours : hours
    minu = minu < 10 ? '0' + minu : minu
    second = second < 10 ? '0' + second : second
    var formatArr = [
      fTime.getFullYear().toString(),
      month.toString(),
      day.toString(),
      hours.toString(),
      minu.toString(),
      second.toString()
    ]
    for (var i = 0; i < formatArr.length; i++) {
      formatStr = formatStr.replace(fStr.charAt(i), formatArr[i])
    }
    return formatStr
  } else {
    return ''
  }
}

export function formatter(type, val) {
  if (type === 'year') {
    return val + '年'
  }
  if (type === 'month') {
    return val + '月'
  }
  if (type === 'day') {
    return val + '日'
  }
  return val
}

export function formatterDate(value) {
  const date = new Date(value)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0') // 月份从0开始，需要加1
  const day = String(date.getDate()).padStart(2, '0')

  return `${year}-${month}-${day}`
}

export function formatterDateHourMinute(date) {
  var year = date.getFullYear();
  var month = date.getMonth() + 1; // getMonth() 返回的月份是从 0 开始的
  var day = date.getDate();
  var hour = date.getHours();
  var minute = date.getMinutes();

  // 如果需要补零操作，可以添加相应的逻辑
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;
  hour = hour < 10 ? '0' + hour : hour;
  minute = minute < 10 ? '0' + minute : minute;

  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
}

export function formatTotalDate(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1
  const day = String(date.getDate()).padStart(2, '0');
  const hours = date.getHours().toString().padStart(2, '0');
  const minutes = date.getMinutes().toString().padStart(2, '0');
  const seconds = date.getSeconds().toString().padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}


// 根据value返回label
export function valueTolabel(value, data, formatter = { value: 'value', label: 'text' }) {
  if (!data?.length) {
    return ''
  }
  let label = ''
  data.map((item) => {
    if (item[formatter.value] == value) {
      label = item[formatter.label]
    }
  })
  return label
}

export function formatDay(newDate) {
  let month = newDate.getMonth() + 1
  let day = newDate.getDate()
  return newDate.getFullYear() + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day)
}

export function formatTime(newDate, ss = true) {
  let hours = newDate.getHours()
  let minutes = newDate.getMinutes()
  let seconds = newDate.getSeconds()
  hours = hours < 10 ? '0' + hours : hours
  minutes = minutes < 10 ? '0' + minutes : minutes
  seconds = seconds < 10 ? '0' + seconds : seconds
  return hours + ':' + minutes + (ss ? ':' + seconds : '')
}
export function formatWeek(newDate) {
  const week = newDate.getDay()
  return '周' + ['日', '一', '二', '三', '四', '五', '六'][week]
}

export function isArray(value) {
  return Array.isArray(value);
}